/*
 * checkout.java
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**This action class adds the transactionLine to the revenue Source
 *
 * @author Kendall Frazier
 */
public class checkout implements edu.byu.isys413.web.Action {
    
    /**
     *Creates a new instance of checkout
     *
     */
    public checkout() {
    }
  
    /** 
     *Processes adds an item to the shopping cart.  It takes in the SKU and quantity of the iteme, and adds a new Transaction Line
     *and Revenue Source to the current Transaction.  The transaction is saved to the session, and sent to the cart2.jsp to work
     *
     *@param HttpServletRequest
     *@param HttpServletResponse
     *
     *@return String the name of the jsp to call
     */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        PrintHandler pHandler = new PrintHandler();
        SaleHandler sHandler = SaleHandler.getInstance();
        TransactionDAO TXDAO = TransactionDAO.getInstance();
        TransLineDAO TLDAO = TransLineDAO.getInstance();
        
        //read in parameters and create sale RevSrc
        String SKU = request.getParameter("SKU");
        int qty = Integer.parseInt(request.getParameter("qty"));
                
        String name = null;
        if(request.getParameter("name")==null){
            name = "IT DIDN'T WORK";
        }else{
            name = request.getParameter("name");
        }
        
        SaleBO saleBO = sHandler.addProductToSale(SKU, qty);
        
        //create & populate Transaction        
        TransactionBO TXBO = null;
        if(session.getAttribute("TX")==null)
        {
            TXBO = TXDAO.create();
            CustomerBO customer = (CustomerBO)session.getAttribute("customer");
            TXBO.setCustomerID(customer.getCustID());
            TXBO.setStoreID("0000011106a3cc370000007f000002");
            TXBO.setEmployeeID("0000011106a3cc390000007f000002");
            //set the date for the transaction
            Calendar cal = Calendar.getInstance();
            Date today = cal.getTime();
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
            String todayDate = fmt.format(today);
            TXBO.setTransDate(todayDate);
        }else{
            TXBO = (TransactionBO)session.getAttribute("TX");
        }
        
        
        TransLineBO TLBO = TLDAO.create();
        TLBO.setRevSrc(saleBO);
        TLBO.setTypeRS("sale");
        TLBO.calcLineAmount();
        TLBO.setTransactionID(TXBO.getTransID());
        
        TXBO.addTxLine(TLBO);
        
        TXBO.calcSubTotal();
        TXBO.calcTax();        
        TXBO.calcTotal();
        TXBO.setTransStatus("complete");
        
        request.setAttribute("TX", TXBO);
                
        session.setAttribute("TX", TXBO);        
        return "cart2.jsp";
    }//process
    
    
}//checkout class
